package main

import (
	"answer5/adb"
	"answer5/log"
	"answer5/unti"
	"answer5/weishi"
	"answer5/xiaohongshu"
	"fmt"
	"golang.org/x/net/websocket"
	"net/http"
	"os"
)

var(
	isStart bool
	webs  *websocket.Conn
	app string
)

func main(){
	fmt.Println("start")
	go log.LogPrint("start")
	//ttt()
	http.Handle("/answer", websocket.Handler(answer))
	http.HandleFunc("/", index)
	fmt.Println("请用chrome访问 http://127.0.0.1:8818")

	adb.StartUiautomator()

	//test.TestClick()
	//test.TestXiGuaNewQuestion()
	//test.TestKuaiShouNewQuestion()
	//test.TestRandomOptions()
	//test.TestImg()
	//test.TestXiaoYuan()

	if err := http.ListenAndServe(":8818", nil); err != nil {
		fmt.Println(err)
		go log.ErrorPrint(err.Error())
		os.Exit(1)
	}





}
func index(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		return
	}
	if r.URL.Path != "/" {
		http.ServeFile(w, r, r.URL.Path[1:])
	} else {
		http.ServeFile(w, r, "index.html")
	}
}
func answer(ws *websocket.Conn){
	webs = ws
	unti.StartWs(ws,start,stop)
}

func stop() {
	isStart=false
	if app == "xiaohongshu" {
		go xiaohongshu.Stop(&isStart)
	}else if app == "weishi" {
		go weishi.Stop(&isStart)
	}

}
func start(_app string) {
	isStart = true
	app=_app
	//adb.Restart()
	if app == "xiaohongshu" {
		go xiaohongshu.Start(&isStart)
	}else if app == "weishi" {
		go weishi.Start(&isStart)
	}
}
func init(){
	adb.Restart()
}